//=============================================================================

#include "../inc/aGround.h"

//=============================================================================

aGround::aGround()
{
  xStep = 0.25f;
  zStep = 0.25f;
  heightLimit = 50.0f;
}

//=============================================================================

agdRet aGround::agLoadHeightMapTGA(const aString &path)
{
  if (path.asGetLength() <= 0)
    return GRO_NULLED_PATH;

  asRemoveSurfaceData();
  asRemoveSkinData();

  if (!atLoadTexture(path))
    return GRO_CANT_LOAD_HEIGHT_MAP;

  asLoadSkin(path);

  return agCompileByHeightMap(1);
}

//=============================================================================

void aGround::agGenerateHeightMap(unsigned int x, unsigned int y, float step)
{
  //float minX = 0 - float(x)/2, maxX = float(x)/2;
  //float minZ = 0 - float(y)/2, maxZ = float(y)/2;
  //long i, steps;
  //float add;
}

//=============================================================================

agdRet aGround::agCompileByHeightMap(bool isUV)
{
  if (atIsNULL())
    return GRO_CANT_COMPILE_NO_HEIGHTMAP;

  unsigned short i, j, k;
  unsigned int tmpNum[4];
  unsigned long num = 0, num2 = 0;
  float x = -(float(width)*(xStep))/2.0f;
  float z = (float(height)*(zStep))/2.0f;
  float uStep = 1.0f/float(width);
  float vStep = 1.0f/float(height);

  asSpawnMemory(width*height*2);
  for (i = 0; i < (height - 1); ++i)
    for (j = 0; j < (width - 1); ++j)
    {
      tmpNum[0] = 0;
      tmpNum[1] = 0;
      tmpNum[2] = 0;
      tmpNum[3] = 0;
      
      for (k = 0; k < channels; ++k)
      {
        tmpNum[0] += data[i*width*channels + (j + 1)*channels + k];
        tmpNum[1] += data[(i + 1)*width*channels + (j + 1)*channels + k];
        tmpNum[2] += data[(i + 1)*width*channels + j*channels + k];
        tmpNum[3] += data[i*width*channels + j*channels + k];
      }

      vertexes[num++] = x + float(j + 1)*xStep;
      vertexes[num++] = (float(tmpNum[0])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i)*zStep;

      vertexes[num++] = x + float(j + 1)*xStep;
      vertexes[num++] = (float(tmpNum[1])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i + 1)*zStep;

      vertexes[num++] = x + float(j)*xStep;
      vertexes[num++] = (float(tmpNum[2])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i + 1)*zStep;

      vertexes[num++] = x + float(j + 1)*xStep;
      vertexes[num++] = (float(tmpNum[0])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i)*zStep;

      vertexes[num++] = x + float(j)*xStep;
      vertexes[num++] = (float(tmpNum[2])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i + 1)*zStep;

      vertexes[num++] = x + float(j)*xStep;
      vertexes[num++] = (float(tmpNum[3])/float(channels) - 128.0f)/heightLimit;
      vertexes[num++] = z - float(i)*zStep;

      if (isUV)
      {
        uv[num2++] = float(j + 1)*uStep;
        uv[num2++] = float(i)*vStep;

        uv[num2++] = float(j + 1)*uStep;
        uv[num2++] = float(i + 1)*vStep;

        uv[num2++] = float(j)*uStep;
        uv[num2++] = float(i + 1)*vStep;

        uv[num2++] = float(j + 1)*uStep;
        uv[num2++] = float(i)*vStep;

        uv[num2++] = float(j)*uStep;
        uv[num2++] = float(i + 1)*vStep;

        uv[num2++] = float(j)*uStep;
        uv[num2++] = float(i)*vStep;
      }
    }

  //asGenerateNormals();

  return GRO_DONE;
}

//=============================================================================

aGround::~aGround()
{
  asRemoveSurfaceData();
}

//=============================================================================
